package com.sgb.kjwl.view.ui.fragment.self;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentLeaseDemandStateBinding;
import com.sgb.kjwl.databinding.FragmentLeaseOrderStateBinding;
import com.sgb.kjwl.di.self.DaggerSelfFragComponent;
import com.sgb.kjwl.di.self.SelfFragModule;
import com.sgb.kjwl.model.entity.lease.LeaseDemandListEntity;
import com.sgb.kjwl.model.entity.lease.LeaseOrderListEntity;
import com.sgb.kjwl.view.event.LeaseOrderEvent;
import com.sgb.kjwl.view.ui.adapter.machine.LeaseDemandAdapter;
import com.sgb.kjwl.view.ui.adapter.self.publish.LeaseOrderStateAdapter;
import com.sgb.kjwl.viewmodel.self.publish.LeaseDemandStateModel;
import com.sgb.kjwl.viewmodel.self.publish.LeaseOrderStateModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

/**
 * 租赁-快捷需求列表
 */
public class LeaseDemandStateFragment extends AppLazyFragment {
    private FragmentLeaseDemandStateBinding binding;
    @Inject
    LeaseDemandStateModel viewModel;
    private String status = "";
    private LeaseDemandAdapter adapter;

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerSelfFragComponent.builder()
                .appComponent(appComponent)
                .selfFragModule(new SelfFragModule(this))
                .build()
                .inject(this);
    }

    public static LeaseDemandStateFragment newInstance(String status) {
        LeaseDemandStateFragment fragment = new LeaseDemandStateFragment();
        Bundle bundle = new Bundle();
        bundle.putString("status", status);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_lease_demand_state, container, false);
        iniArguments();
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

    }

    @Override
    protected void initView() {
        if (adapter == null) {
            adapter = new LeaseDemandAdapter(getActivity());
        }
        adapter.setOnCallBlackListener(new LeaseDemandAdapter.OnCallBlackListener() {
            @Override
            public void onDel(String no) {
                showDeleteDialog(no);
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.rlvView.setLayoutManager(layoutManager);
        binding.rlvView.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getResources().getDimension(R.dimen.dp10), getResources().getColor(R.color.color_F5F5F5)));
        binding.rlvView.setAdapter(adapter);
        binding.rlvView.setLoadingMoreEnabled(true);
        binding.rlvView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getLeaseOrederListModel(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getLeaseOrederListModel(true);
            }
        });

    }

    @Override
    protected void onLazyLoad() {
        initData();
    }

    private void iniArguments() {
        status = (String) getArguments().get("status");
    }

    private void initData() {
        viewModel.map.get().put("status", status);
        viewModel.getLeaseOrederListModel(false);
    }

    public void showList(List<LeaseDemandListEntity> datas, boolean isLoadMore) {
        if (isLoadMore) {
            binding.rlvView.loadMoreComplete();
            binding.rlvView.setNoMore(datas.size() < viewModel.pageSize);
            if (datas.size() > 0) {
                adapter.addDatas(datas);
            }
        } else {
            if (datas.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.rlvView.setNoMore(datas.size() < viewModel.pageSize);
            binding.rlvView.refreshComplete();
            binding.rlvView.setLoadingMoreEnabled(true);
            adapter.setDatas(datas);
        }
    }

    public void onLoadComplete(boolean isLoadMore) {
        binding.emptyView.setVisibility(View.GONE);
        if (isLoadMore) {
            binding.rlvView.loadMoreComplete();
        } else {
            binding.rlvView.refreshComplete();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LeaseOrderEvent event) {//刷新
        viewModel.getLeaseOrederListModel(false);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void showDeleteDialog(String no) {
        new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                .setTitle("删除提示")
                .setMsg("您确定要删除当前信息？ 删除后，将不可恢复")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        viewModel.delModel(no);
                    }

                    @Override
                    public void onLeftClick() {

                    }
                }).show();
    }
}
